/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 复选框视图
    /// </summary>
    public class FCCheckBox : FCButton {
        /// <summary>
        /// 创建复选框
        /// </summary>
        public FCCheckBox() {
            setBackColor(FCColor.None);
            setBorderColor(FCColor.None);
        }


        public FCHorizontalAlign m_buttonAlign = FCHorizontalAlign.Left;

        /// <summary>
        /// 获取内容的横向排列样式
        /// </summary>
        public virtual FCHorizontalAlign getButtonAlign() {
            return m_buttonAlign;
        }

        /// <summary>
        /// 设置内容的横向排列样式
        /// </summary>
        public virtual void setButtonAlign(FCHorizontalAlign value) {
            m_buttonAlign = value;
        }

        public long m_buttonBackColor = FCColor.Border;

        /// <summary>
        /// 获取按钮的背景色
        /// </summary>
        public virtual long getButtonBackColor() {
            return m_buttonBackColor;
        }

        /// <summary>
        /// 设置按钮的背景色
        /// </summary>
        public virtual void setButtonBackColor(long value) {
            m_buttonBackColor = value;
        }

        public long m_buttonBorderColor = FCColor.Border;

        /// <summary>
        /// 获取按钮的边线颜色
        /// </summary>
        public long getButtonBorderColor() {
            return m_buttonBorderColor;
        }

        /// <summary>
        /// 设置按钮的边线颜色
        /// </summary>
        public virtual void setButtonBorderColor(long value) {
            m_buttonBorderColor = value;
        }

        public FCSize m_buttonSize = new FCSize(16, 16);

        /// <summary>
        /// 获取按钮的尺寸
        /// </summary>
        public virtual FCSize getButtonSize() {
            return m_buttonSize;
        }

        /// <summary>
        /// 设置按钮的尺寸
        /// </summary>
        public virtual void setButtonSize(FCSize value) {
            m_buttonSize = value;
        }

        public bool m_checked = false;

        /// <summary>
        /// 获取是否被选中
        /// </summary>
        public virtual bool isChecked() {
            return m_checked;
        }

        /// <summary>
        /// 设置是否被选中
        /// </summary>
        public virtual void setChecked(bool value) {
            if (m_checked != value) {
                m_checked = value;
                onCheckedChanged();
            }
        }

        public String m_checkedBackImage;

        /// <summary>
        /// 获取选中时的背景图
        /// </summary>
        public virtual String getCheckedBackImage() {
            return m_checkedBackImage;
        }

        /// <summary>
        /// 设置选中时的背景图
        /// </summary>
        public virtual void setCheckedBackImage(String value) {
            m_checkedBackImage = value;
        }

        public String m_checkHoveredBackImage;

        /// <summary>
        /// 获取触摸悬停且选中时的背景图片
        /// </summary>
        public virtual String getCheckHoveredBackImage() {
            return m_checkHoveredBackImage;
        }

        /// <summary>
        /// 设置触摸悬停且选中时的背景图片
        /// </summary>
        public virtual void setCheckHoveredBackImage(String value) {
            m_checkHoveredBackImage = value;
        }

        public String m_checkPushedBackImage;

        /// <summary>
        /// 获取触摸按下且选中时的背景图片
        /// </summary>
        public virtual String getCheckPushedBackImage() {
            return m_checkPushedBackImage;
        }

        /// <summary>
        /// 设置触摸按下且选中时的背景图片
        /// </summary>
        public virtual void setCheckPushedBackImage(String value) {
            m_checkPushedBackImage = value;
        }

        public String m_disableCheckedBackImage;

        /// <summary>
        /// 获取不可用时的选中背景图片
        /// </summary>
        public virtual String getDisableCheckedBackImage() {
            return m_disableCheckedBackImage;
        }

        /// <summary>
        /// 设置不可用时的选中背景图片
        /// </summary>
        public virtual void setDisableCheckedBackImage(String value) {
            m_disableCheckedBackImage = value;
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "CheckBox";
        }

        /// <summary>
        /// 获取事件名称列表
        /// </summary>
        /// <returns>名称列表</returns>
        public override ArrayList<String> getEventNames() {
            ArrayList<String> eventNames = base.getEventNames();
            eventNames.AddRange(new String[] { "CheckedChanged" });
            return eventNames;
        }

        /// <summary>
        /// 获取或设置的背景色
        /// </summary>
        /// <returns>背景色</returns>
        public override long getPaintingBackColor() {
            long backColor = getBackColor();
            if (backColor != FCColor.None && FCColor.DisabledBack != FCColor.None) {
                if (!isPaintEnabled(this)) {
                    return FCColor.DisabledBack;
                }
            }
            return backColor;
        }

        /// <summary>
        /// 获取要绘制的按钮的背景色
        /// </summary>
        /// <returns>按钮的背景色</returns>
        public virtual long getPaintingButtonBackColor() {
            long buttonBackColor = m_buttonBackColor;
            if (buttonBackColor != FCColor.None && FCColor.DisabledBack != FCColor.None) {
                if (!isPaintEnabled(this)) {
                    return FCColor.DisabledBack;
                }
            }
            return buttonBackColor;
        }

        /// <summary>
        /// 获取要绘制的按钮边线颜色
        /// </summary>
        /// <returns>边线的颜色</returns>
        public virtual long getPaintingButtonBorderColor() {
            return m_buttonBorderColor;
        }

        /// <summary>
        /// 获取用于绘制的背景图片
        /// </summary>
        public override String getPaintingBackImage() {
            String backImage = null;
            FCNative native = getNative();
            if (m_checked) {
                if (isEnabled()) {
                    if (this == native.getPushedView()) {
                        backImage = m_checkPushedBackImage;
                    } else if (this == native.getHoveredView()) {
                        backImage = m_checkHoveredBackImage;
                    } else {
                        backImage = m_checkedBackImage;
                    }
                } else {
                    backImage = m_disableCheckedBackImage;
                }
            }
            if (backImage != null) {
                return backImage;
            } else {
                return base.getPaintingBackImage();
            }
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">返回属性值</param>
        /// <param name="type">返回属性类型</param>
        public override void getAttribute(String name, ref String value, ref String type) {
            switch (name)
            {
                case "buttonalign":
                    type = "enum:FCHorizontalAlign";
                    value = FCTran.horizontalAlignToStr(getButtonAlign());
                    break;
                case "buttonbackcolor":
                    type = "color";
                    value = FCTran.colorToStr(getButtonBackColor());
                    break;
                case "buttonbordercolor":
                    type = "color";
                    value = FCTran.colorToStr(getButtonBorderColor());
                    break;
                case "buttonsize":
                    type = "size";
                    value = FCTran.sizeToStr(getButtonSize());
                    break;
                case "checked":
                    type = "bool";
                    value = FCTran.boolToStr(isChecked());
                    break;
                case "checkedbackimage":
                    type = "text";
                    value = getCheckedBackImage();
                    break;
                case "checkhoveredbackimage":
                    type = "text";
                    value = getCheckHoveredBackImage();
                    break;
                case "checkpushedbackimage":
                    type = "text";
                    value = getCheckPushedBackImage();
                    break;
                case "disablecheckedbackimage":
                    type = "text";
                    value = getDisableCheckedBackImage();
                    break;
                default:
                    base.getAttribute(name, ref value, ref type);
                    break;
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns>属性名称列表</returns>
        public override ArrayList<String> getAttributeNames() {
            ArrayList<String> attributeNames = base.getAttributeNames();
            attributeNames.AddRange(new String[] { "ButtonAlign", "ButtonBackColor","ButtonBorderColor", "ButtonSize", "Checked", "CheckedBackImage",
            "CheckHoveredBackimage", "CheckPushedBackImage", "DisableCheckedBackImage"});
            return attributeNames;
        }

        /// <summary>
        /// 选中改变时触发
        /// </summary>
        public virtual void onCheckedChanged() {
            callEvents("oncheckedchanged");
            update();
        }

        /// <summary>
        /// 点击方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onClick(FCTouchInfo touchInfo) {
            setChecked(!isChecked());
            callTouchEvents("onclick", touchInfo);
            invalidate();
        }

        /// <summary>
        /// 重绘背景方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public override void onPaintBackground(FCPaint paint, FCRect clipRect) {
            FCRect rect = new FCRect(0, 0, getWidth(), getHeight());
            //绘制背景色
            paint.fillRoundRect(getPaintingBackColor(), rect, getCornerRadius());
        }

        /// <summary>
        /// 重绘选中按钮方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public virtual void onPaintCheckButton(FCPaint paint, FCRect clipRect) {
            //绘制背景图
            String bkImage = getPaintingBackImage();
            if (bkImage != null && bkImage.Length > 0) {
                paint.drawImage(bkImage, clipRect);
            } else {
                if (m_checked) {
                    FCRect innerRect = new FCRect(clipRect.left + 2, clipRect.top + 2, clipRect.right - 3, clipRect.bottom - 3);
                    if (clipRect.right - clipRect.left < 4 || clipRect.bottom - clipRect.top < 4) {
                        innerRect = clipRect;
                    }
                    FCPoint[] pp = new FCPoint[3];
                    pp[0] = new FCPoint(innerRect.left, innerRect.top + 8);
                    pp[1] = new FCPoint(innerRect.left + 6, innerRect.bottom);
                    pp[2] = new FCPoint(innerRect.right - 1, innerRect.top);
                    paint.drawPolyline(getPaintingTextColor(), 1, 0, pp);
                }
                paint.drawRect(getPaintingButtonBorderColor(), 1, 0, clipRect);
            }
        }

        /// <summary>
        /// 重绘前景方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public override void onPaintForeground(FCPaint paint, FCRect clipRect) {
            String text = getText();
            int width = getWidth(), height = getHeight();
            if (width > 0 && height > 0) {
                FCRect buttonRect = new FCRect(5, (height - m_buttonSize.cy) / 2, 5 + m_buttonSize.cx, (height + m_buttonSize.cy) / 2);
                FCPoint tLocation = new FCPoint();
                FCSize tSize = new FCSize();
                FCFont font = getFont();
                if (text != null && text.Length > 0) {
                    tSize = paint.textSize(text, font, -1);
                    tLocation.x = buttonRect.right + 5;
                    tLocation.y = (height - tSize.cy) / 2;
                }
                //居中
                if (m_buttonAlign == FCHorizontalAlign.Center) {
                    buttonRect.left = (width - m_buttonSize.cx) / 2;
                    buttonRect.right = (width + m_buttonSize.cx) / 2;
                    tLocation.x = buttonRect.right + 5;
                }
                    //远离
                else if (m_buttonAlign == FCHorizontalAlign.Right) {
                    buttonRect.left = width - m_buttonSize.cx - 5;
                    buttonRect.right = width - 5;
                    tLocation.x = buttonRect.left - tSize.cx - 5;
                }
                //绘制背景图
                onPaintCheckButton(paint, buttonRect);
                //绘制文字
                if (text != null && text.Length > 0) {
                    FCRect tRect = new FCRect(tLocation.x, tLocation.y, tLocation.x + tSize.cx + 1, tLocation.y + tSize.cy);
                    long textColor = getPaintingTextColor();
                    if (autoEllipsis() && (tRect.right > clipRect.right || tRect.bottom > clipRect.bottom)) {
                        if (tRect.right > clipRect.right) {
                            tRect.right = clipRect.right;
                        }
                        if (tRect.bottom > clipRect.bottom) {
                            tRect.bottom = clipRect.bottom;
                        }
                        paint.drawTextAutoEllipsis(text, textColor, font, tRect);
                    } else {
                        paint.drawText(text, textColor, font, tRect, -1);
                    }
                }
            }
        }

        /// <summary>
        /// 缩放
        /// </summary>
        /// <param name="scaleFactorX">横向比例</param>
        /// <param name="scaleFactorY">纵向比例</param>
        public override void onScaling(double scaleFactorX, double scaleFactorY) {
            base.onScaling(scaleFactorX, scaleFactorY);
            m_buttonSize = new FCSize((int)(m_buttonSize.cx * scaleFactorX), (int)(m_buttonSize.cy * scaleFactorY));
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">属性值</param>
        public override void setAttribute(String name, String value)
        {
            switch (name)
            {
                case "buttonalign":
                    setButtonAlign(FCTran.strToHorizontalAlign(value));
                    break;
                case "buttonbackcolor":
                    setButtonBackColor(FCTran.strToColor(value));
                    break;
                case "buttonbordercolor":
                    setButtonBorderColor(FCTran.strToColor(value));
                    break;
                case "buttonsize":
                    setButtonSize(FCTran.strToSize(value));
                    break;
                case "checked":
                    setChecked(FCTran.strToBool(value));
                    break;
                case "checkedbackimage":
                    setCheckedBackImage(value);
                    break;
                case "checkhoveredbackimage":
                    setCheckHoveredBackImage(value);
                    break;
                case "checkpushedbackimage":
                    setCheckPushedBackImage(value);
                    break;
                case "disablecheckedbackimage":
                    setDisableCheckedBackImage(value);
                    break;
                default:
                    base.setAttribute(name, value);
                    break;
            }
        }
    }
}
